package org.scboy.common.request;

import java.util.List;

import org.scboy.common.request.inter.Inter;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;

/**
 * @author 四川耗子
 * @email 125167488@qq.com
 * @date 2017年9月14日
 */
public class Result {
	
	private boolean success;

	private Long total;
	private String message;
	private String msgCode;
	
	private int status;

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	private Object data;

	public static Result Success() {
		return Success(null, null, null);
	}
	
	public static Result Success(Page page) {
		return Success(page.getData(), page.getCount());
	}
	
	public static Result Success(Model data) {
		return Success(data,0L);
	}
	
	public static Result Success(List<Model> datas) {
		return Success(datas, 0L);
	}
	
	public static Result Success(String code) {
		return Success(null, code, null);
	}

	public static Result Success(Object data, Long total) {
		return Success(data, null, total);
	}

	public static Result Success(Object data, String code) {
		return Success(data, code, null);
	}

	/**
	 * 返回成功。
	 * 
	 * @param data
	 * @param msg
	 * @param total
	 * @return
	 */
	public static Result Success(Object data, String code, Long total) {
		Result result = new Result();
		result.success = true;
		result.setData(data);
		result.msgCode=code;
		result.setMessage(Inter.msg(code));
		result.setTotal(total);
		result.setStatus(200);
		return result;
	}

	public static Result Error() {
		return Error(null, null);
	}

	public static Result Error(String code) {
		return Error(code, null);
	}

	/**
	 * 请求错误
	 * 
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result Error(String code, Object data) {
		Result result = new Result();
		result.success = false;
		result.setData(data);
		result.msgCode=code;
		result.setMessage(Inter.msg(code));
		result.setStatus(500);
		return result;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Long getTotal() {
		return total;
	}

	public void setTotal(Long total) {
		this.total = total;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
	
	/**
	 * 未登录
	 * @param msg
	 * @return
	 */
	public static Result logged(String code){
		return logged(code, null);
	}
	/**
	 * 未登录
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result logged(String code,String data){
		Result result = new Result();
		result.success = false;
		result.setData(data);
		result.msgCode=code;
		result.setMessage(Inter.msg(code));
		result.setStatus(401);
		return result;
	}
	
	/**
	 * 未授权
	 * 
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result Unauthorized(String code){
		return Unauthorized(code, null);
	}
	
	/**
	 * 未授权
	 * 
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result Unauthorized(String code, Object data) {
		Result result = new Result();
		result.success = false;
		result.setData(data);
		result.msgCode=code;
		result.setMessage(Inter.msg(code));
		result.setStatus(407);
		return result;
	}
	
	/**
	 * 
	 * 验证不通过
	 * 
	 * @param msg
	 * @param data
	 * @return
	 */
	public static Result ValidError(String code, Object data) {
		Result result = new Result();
		result.success = false;
		result.setData(data);
		result.msgCode=code;
		result.setMessage(Inter.msg(code));
		result.setStatus(403);
		return result;
	}
	
	
	public Result msgMapping(Model params) {
		this.setMessage(Inter.msg(this.msgCode,params));
		return this;
	}
	
}
